package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;
import soundCapture.Capture;

public class Task extends JFrame {
	JPanel mpanel = new JPanel();
	JPanel panel1 = new JPanel();
	JButton bback = new JButton("Back");
	JButton baudio_play = new JButton("Play");
	JButton baudio_stop = new JButton("Stop");

	Capture audio = new Capture();

	public Task(File f) {
		getTask(f);
	}

	public void getTask(File f) {
		setSize(600, 400);
		setLocation(500, 280);
		setResizable(false);
		panel1.setLayout(null);
		try {
			Scanner scanFile = new Scanner(f);
			JLabel title = new JLabel("Title:");
			JLabel date = new JLabel("Date:");
			JLabel group = new JLabel("Group:");
			JLabel details = new JLabel("Details:");
			JLabel txtitle = new JLabel(scanFile.nextLine());
			JLabel txdate = new JLabel(scanFile.nextLine());
			JLabel txgroup = new JLabel(scanFile.nextLine());
			JTextArea txdetails = new JTextArea();
			String detail = scanFile.nextLine();
					while(scanFile.hasNext())
						detail += "\n" + scanFile.nextLine();
			txdetails.setText(detail);
			JScrollPane scroll = new JScrollPane(txdetails,
					JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

			title.setBounds(20, 20, 150, 20);
			date.setBounds(20, 70, 150, 20);
			group.setBounds(20, 120, 150, 20);
			details.setBounds(20, 170, 150, 20);

			txtitle.setBounds(100, 20, 200, 20);
			txdate.setBounds(100, 70, 200, 20);
			txgroup.setBounds(100, 120, 200, 20);
			scroll.setBounds(100, 170, 160, 150);
			txdetails.setLineWrap(true);
			txdetails.setWrapStyleWord(true);

			panel1.add(title);
			panel1.add(date);
			panel1.add(group);
			panel1.add(details);
			panel1.add(txtitle);
			panel1.add(txdate);
			panel1.add(scroll);
			panel1.add(txgroup);
			String imagePath = f.getAbsolutePath().replace(".txt", ".jpg");
			File image = new File(imagePath);
			String audioPath = f.getAbsolutePath().replace(".txt", ".wav");
			File audioFile = new File(audioPath);
			if (image.exists()) {
				Toolkit toolkit = Toolkit.getDefaultToolkit();
				Image Image1 = toolkit.getImage(image.getAbsolutePath());
				ImageIcon icon = new ImageIcon(Image1);
				int height = icon.getIconHeight();
				int width = icon.getIconWidth();
				double ratio = 1;
				if (height > width && height > 350)
					ratio = (new Double(350)).doubleValue() / height;
				else if (height < width && width > 325)
					ratio = (new Double(325)).doubleValue() / width;
				height *= ratio;
				width *= ratio;
				Image1 = Image1.getScaledInstance(width, height,
						Image.SCALE_SMOOTH);
				JLabel picLabel = new JLabel(new ImageIcon(Image1));
				picLabel.setBounds(250, 20, 325, 350);
				panel1.add(picLabel);
			}
			if (audioFile.exists()) {
				audio.openAudio(audioPath);
				baudio_play.setBounds(15, 250, 80, 20);
				baudio_stop.setBounds(15, 280, 80, 20);
				panel1.add(baudio_stop);
				panel1.add(baudio_play);

			}
			scanFile.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		bback.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				dispose();
			}
		});

		baudio_stop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				audio.stopAudio();
			}
		});

		baudio_play.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (!audio.isPlaying())
					audio.playAudio();
			}
		});

		mpanel.setLayout(new BorderLayout());
		mpanel.add(panel1, BorderLayout.CENTER);
		mpanel.add(bback, BorderLayout.SOUTH);
		getContentPane().add(mpanel);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
	}

}
